package cloudspace.controlpanel.submitter;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.springframework.security.core.context.SecurityContextHolder;
import org.webcat.submitter.SubmissionManifest;
import org.webcat.submitter.SubmissionTargetException;
import org.webcat.submitter.Submitter;
import org.webcat.submitter.targets.AssignmentTarget;
import org.webcat.submitter.targets.SubmissionTarget;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericComposer;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Window;

import cloudspace.config.ServletLocationFinder;
import cloudspace.security.CloudSpaceUser;
import cloudspace.vm.filesystem.CSPath;


public class SubmitFilesController extends GenericComposer
{
    /**
	 * 
	 */
    private static final long serialVersionUID = 5295402992765947073L;

    private Window submitWindow;

    private Tree targetTree;

    private Submitter submitter;

    private Set<CSPath> selectedPaths;

    Component self;


    @Override
    @SuppressWarnings("unchecked")
    public void doAfterCompose( Component component ) throws Exception
    {
        super.doAfterCompose( component );
        self = component;
        submitWindow = (Window)component;
        targetTree = (Tree)component.getFellow( "targetTree" );

        selectedPaths = (Set<CSPath>)Executions.getCurrent()
            .getArg()
            .get( "selectedPaths" );

        submitter = new Submitter();

        File internalTargetFile = new File( ServletLocationFinder.getServletResourcePath( "/configs/submissionTarget.xml" ) );
        submitter.readSubmissionTargets( internalTargetFile.toURI().toURL() );
        targetTree.setModel( new SubmissionTargetTreeModel( submitter.getRoot() ) );
        targetTree.setTreeitemRenderer( new SubmissionTreeitemRenderer() );
    }


    public void onOK( Event event )
    {
        Object value = targetTree.getSelectedItem().getValue();
        if ( !( value instanceof SubmissionTarget ) )
            return;
        self.setAttribute( "ok", true );

        SubmissionTarget target = (SubmissionTarget)value;

        SubmissionManifest manifest = new SubmissionManifest();
        manifest.setAssignment( (AssignmentTarget)target );

        SubmittableCSPath[] subPaths = new SubmittableCSPath[selectedPaths.size()];
        int i = 0;
        for ( CSPath path : selectedPaths )
        {
            subPaths[i++] = new SubmittableCSPath( path );
        }

        manifest.setSubmittableItems( subPaths );

        CloudSpaceUser auth = (CloudSpaceUser)SecurityContextHolder.getContext()
            .getAuthentication()
            .getPrincipal();
        manifest.setUsername( auth.getUsername() );
        manifest.setPassword( auth.getPassword() );

        try
        {
            submitter.submit( manifest );
        }
        catch ( SubmissionTargetException e )
        {
            e.printStackTrace();
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        submitWindow.detach();

    }

}
